<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-light">
          <h3>产品申请信息详情</h3>
          <template>
            <el-table :data="productAppList" stripe style="width: 100%">
              <el-table-column prop="productName" label="产品名称" width="120">
              </el-table-column>
              <el-table-column prop="productDesc" label="产品简介" width="180">
              </el-table-column>
              <el-table-column
                prop="productPicture"
                label="注册资金（元）"
                width="120"
              >
              </el-table-column>
              <el-table-column
                prop="productAmount"
                label="放贷额度（元）"
                width="120"
              >
              </el-table-column>
              <el-table-column prop="averageName" label="还款方式" width="100">
              </el-table-column>
              <el-table-column
                prop="productTypeName"
                label="产品类型"
                width="100"
              >
              </el-table-column>
              <el-table-column prop="productBy1" label="公司名称" width="120">
              </el-table-column>
              <el-table-column
                prop="productBy2"
                label="账户所属银行"
                width="120"
              >
              </el-table-column>
              <el-table-column prop="productFlag" label="申请状态" width="100">
              </el-table-column>
              <el-table-column prop="address" label="操作" width="200">
                <template slot-scope="scope">
                  <el-button
                    type="success"
                    plain
                    @click="approval(scope.row.productId)"
                    >审批</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </template>
        </div></el-col
      ></el-row
    >
    <!-- 分页插件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currentPage"
      :page-sizes="pageInfo.pageSizes"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>

    <el-dialog title="产品审批" :visible.sync="dialog.approvalshow" width="30%">
      <el-form
        :rules="rules" 
        ref="approvalForm"
        :model="approvalForm"
      >
        <el-form-item label="产品名称" :label-width="formLabelWidth">
          <el-input
            v-model="approvalForm.productName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品介绍" :label-width="formLabelWidth">
          <el-input
            v-model="approvalForm.productDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否同意" :label-width="formLabelWidth">
          <div>
            <el-radio
              v-model="approvalForm.productrecordResult"
              label="1"
              border
              >同意</el-radio
            >
            <el-radio
              v-model="approvalForm.productrecordResult"
              label="2"
              border
              >不同意</el-radio
            >
          </div>
        </el-form-item>
        <el-form-item label="审批人" :label-width="formLabelWidth">
          <el-select
            v-model="approvalForm.productrecordPerson"
            placeholder="请选择审批人"
          >
            <el-option
              v-for="(item, index) in approveBody"
              :key="index"
              :label="item.empName"
              :value="item.empId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="审批人意见"
          prop="productrecordLead"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="approvalForm.productrecordLead"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="agree('approvalForm')">提 交</el-button>
        <el-button @click="dialog.approvalshow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      approveBody: [],
      approvalForm: {
        productrecordLead:''
      },
      rules:{
        productrecordLead:[
          {required: true, message: '请填写审批意见', trigger: 'blur' },
          {min:5, message: '至少输入5个字', trigger: 'blur' }
        ]
      },
      formLabelWidth: "30%",
      productAppList: [],
      pageInfo: {
        pageSizes: [5, 10, 20, 50],
        total: 1,
      },
      query: {
        currentPage: 1,
        pageSize: 5,
      },
      dialog: {
        approvalshow: false,
      },
    };
  },

  methods: {

    selectAllApprovalBody() {
      this.$axios.get("/emps/productLeader").then((res) => {
        this.approveBody = res.data.data.leader;
      });
    },

    selectAllProductApp() {
      this.$axios.post("/productApps/AllProductApp", this.query).then((res) => {
        this.productAppList = res.data.data.productAppList;
        this.pageInfo.total = res.data.data.pageInfo;
      });
    },

    handleCurrentChange(currentPage) {
      this.query.currentPage = currentPage;
      this.selectAllProductApp();
    },

    handleSizeChange(size) {
      this.query.pageSize = size;
      this.selectAllProductApp();
    },

    approval(id) {
      this.dialog.approvalshow = true;
      this.$axios.get("/productApps/" + id).then((res) => {
        this.approvalForm = res.data.data.product;
      });
    },

    agree(approvalForm) {
      this.$refs[approvalForm].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            alert('审批失败，请按要求重新填写!');
            console.log('error submit!!');
            return false;
          }
        });
        
      this.$axios
        .post("/productApps/insertProduct", this.approvalForm)
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success("操作成功");
            this.selectAllProductApp();
          } else {
            this.$message.warn("操作失败，请稍后重试");
          }
        });
      this.dialog.approvalshow = false;
      this.selectAllProductApp();
    },
  },

  mounted() {
    this.selectAllProductApp();
    this.selectAllApprovalBody();
  },
};
</script>